package net.adetech.cjg.outerservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.sccba.open.HttpClient;
import net.adetech.cjg.outerservice.utils.BankUtil;
import net.adetech.framework.utils.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

@RestController
public class DemoController {


    @Autowired
    BankUtil bankUtil;

    @RequestMapping("/cjg/demoGetToken")
    public String getToken() {


        try {
            String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date());


            JSONObject requestJson = new JSONObject();
            requestJson.put("yybs","026");  //应用代码 固定填写026
            requestJson.put("cpdm","9990002601");  //产品代码  固定填写9990002601
            requestJson.put("jydm","801101");    //交易处理代码  填写801101
            requestJson.put("sys_seqno","2018073000001"); //请求流水号
            requestJson.put("order_id","2018073000001");  //商户订单号
            requestJson.put("order_title","工本费"); //订单标题
            requestJson.put("order_life","2");  //订单有效期 单位为分钟
            requestJson.put("amt_source","00"); //交易金额来源 固定填写为00-订单上送
            requestJson.put("amount","0.01");  //交易金额
            requestJson.put("amt_currency","CNY"); //交易币种  CNY 人民币
            requestJson.put("trans_datetime",timeStamp.substring(0,14));  //交易时间 YYYYMMDDhhmmss
            requestJson.put("mchnt_cd","816370650940001");  //商户号
            requestJson.put("mchnt_mcc","115094"); //商户类型
            requestJson.put("notify_url","http://adetech.s1.natapp.cc/cjg/payResult"); //地址


            bankUtil.call(requestJson);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    @RequestMapping(value = "/cjg/payResult", name = "application/json")
    public String payResult(@RequestBody String input)
    {

        System.out.println("\n返回报文:");
        System.out.println(input);

        JSONObject inputJson = JSONObject.parseObject(input);
        String dataString = inputJson.getString("data");

        dataString = SerializeUtil.decodeBase64(dataString);

        System.out.println("\n返回报文 data解码后:");
        System.out.println(dataString);


        return "{result:'ok'}";
    }



}
